#!/usr/bin/perl -w

$cdsfile = $ARGV[0];#cds fasta: same sequences as protein fasta, in the same order
open(CDSFILE,$cdsfile);

$output = $ARGV[1];
open(OUTPUT,">$output");

$window = 17;
$half = 8;

@CodonTypes = ();#sequences of codons
push(@CodonTypes, "TTT");push(@CodonTypes, "TTC");push(@CodonTypes, "TTA");push(@CodonTypes, "TTG");push(@CodonTypes, "CTT");push(@CodonTypes, "CTC");push(@CodonTypes, "CTA");push(@CodonTypes, "CTG");push(@CodonTypes, "ATT");push(@CodonTypes, "ATC");push(@CodonTypes, "ATA");push(@CodonTypes, "ATG");push(@CodonTypes, "GTT");push(@CodonTypes, "GTC");push(@CodonTypes, "GTA");push(@CodonTypes, "GTG");push(@CodonTypes, "TCT");push(@CodonTypes, "TCC");push(@CodonTypes, "TCA");push(@CodonTypes, "TCG");push(@CodonTypes, "AGT");push(@CodonTypes, "AGC");push(@CodonTypes, "CCT");push(@CodonTypes, "CCC");push(@CodonTypes, "CCA");push(@CodonTypes, "CCG");push(@CodonTypes, "ACT");push(@CodonTypes, "ACC");push(@CodonTypes, "ACA");push(@CodonTypes, "ACG");push(@CodonTypes, "GCT");push(@CodonTypes, "GCC");push(@CodonTypes, "GCA");push(@CodonTypes, "GCG");push(@CodonTypes, "TAT");push(@CodonTypes, "TAC");push(@CodonTypes, "CAT");push(@CodonTypes, "CAC");push(@CodonTypes, "CAA");push(@CodonTypes, "CAG");push(@CodonTypes, "AAT");push(@CodonTypes, "AAC");push(@CodonTypes, "AAA");push(@CodonTypes, "AAG");push(@CodonTypes, "GAT");push(@CodonTypes, "GAC");push(@CodonTypes, "GAA");push(@CodonTypes, "GAG");push(@CodonTypes, "TGT");push(@CodonTypes, "TGC");push(@CodonTypes, "TGG");push(@CodonTypes, "CGT");push(@CodonTypes, "CGC");push(@CodonTypes, "CGA");push(@CodonTypes, "CGG");push(@CodonTypes, "AGA");push(@CodonTypes, "AGG");push(@CodonTypes, "GGT");push(@CodonTypes, "GGC");push(@CodonTypes, "GGA");push(@CodonTypes, "GGG");push(@CodonTypes, "TAA");push(@CodonTypes, "TAG");push(@CodonTypes, "TGA");

@AA = ();#AA encoded by each codon
push(@AA, "F");push(@AA, "F");push(@AA, "L");push(@AA, "L");push(@AA, "L");push(@AA, "L");push(@AA, "L");push(@AA, "L");push(@AA, "I");push(@AA, "I");push(@AA, "I");push(@AA, "M");push(@AA, "V");push(@AA, "V");push(@AA, "V");push(@AA, "V");push(@AA, "S");push(@AA, "S");push(@AA, "S");push(@AA, "S");push(@AA, "S");push(@AA, "S");push(@AA, "P");push(@AA, "P");push(@AA, "P");push(@AA, "P");push(@AA, "T");push(@AA, "T");push(@AA, "T");push(@AA, "T");push(@AA, "A");push(@AA, "A");push(@AA, "A");push(@AA, "A");push(@AA, "Y");push(@AA, "Y");push(@AA, "H");push(@AA, "H");push(@AA, "Q");push(@AA, "Q");push(@AA, "N");push(@AA, "N");push(@AA, "K");push(@AA, "K");push(@AA, "D");push(@AA, "D");push(@AA, "E");push(@AA, "E");push(@AA, "C");push(@AA, "C");push(@AA, "W");push(@AA, "R");push(@AA, "R");push(@AA, "R");push(@AA, "R");push(@AA, "R");push(@AA, "R");push(@AA, "G");push(@AA, "G");push(@AA, "G");push(@AA, "G");push(@AA, "X");push(@AA, "X");push(@AA, "X");

@distances = ();#window analyzed
until (scalar @distances == $window){push(@distances,"0");}

$count = 0;
@CodonCounts = ();
until ($count == 64){push(@CodonCounts, [@distances]);++$count;}

$count = 0;
@AACounts = ();
until (
	$count == 21){push(@AACounts, [@distances]);++$count;}
	$count = 0; @TTT = ();until ($count == 64){push(@TTT,[@distances]);++$count;}
	$count = 0; @TTC = ();until ($count == 64){push(@TTC,[@distances]);++$count;}
	$count = 0; @TTA = ();until ($count == 64){push(@TTA,[@distances]);++$count;}
	$count = 0; @TTG = ();until ($count == 64){push(@TTG,[@distances]);++$count;}
	$count = 0; @CTT = ();until ($count == 64){push(@CTT,[@distances]);++$count;}
	$count = 0; @CTC = ();until ($count == 64){push(@CTC,[@distances]);++$count;}
	$count = 0; @CTA = ();until ($count == 64){push(@CTA,[@distances]);++$count;}
	$count = 0; @CTG = ();until ($count == 64){push(@CTG,[@distances]);++$count;}
	$count = 0; @ATT = ();until ($count == 64){push(@ATT,[@distances]);++$count;}
	$count = 0; @ATC = ();until ($count == 64){push(@ATC,[@distances]);++$count;}
	$count = 0; @ATA = ();until ($count == 64){push(@ATA,[@distances]);++$count;}
	$count = 0; @ATG = ();until ($count == 64){push(@ATG,[@distances]);++$count;}
	$count = 0; @GTT = ();until ($count == 64){push(@GTT,[@distances]);++$count;}
	$count = 0; @GTC = ();until ($count == 64){push(@GTC,[@distances]);++$count;}
	$count = 0; @GTA = ();until ($count == 64){push(@GTA,[@distances]);++$count;}
	$count = 0; @GTG = ();until ($count == 64){push(@GTG,[@distances]);++$count;}
	$count = 0; @TCT = ();until ($count == 64){push(@TCT,[@distances]);++$count;}
	$count = 0; @TCC = ();until ($count == 64){push(@TCC,[@distances]);++$count;}
	$count = 0; @TCA = ();until ($count == 64){push(@TCA,[@distances]);++$count;}
	$count = 0; @TCG = ();until ($count == 64){push(@TCG,[@distances]);++$count;}
	$count = 0; @AGT = ();until ($count == 64){push(@AGT,[@distances]);++$count;}
	$count = 0; @AGC = ();until ($count == 64){push(@AGC,[@distances]);++$count;}
	$count = 0; @CCT = ();until ($count == 64){push(@CCT,[@distances]);++$count;}
	$count = 0; @CCC = ();until ($count == 64){push(@CCC,[@distances]);++$count;}
	$count = 0; @CCA = ();until ($count == 64){push(@CCA,[@distances]);++$count;}
	$count = 0; @CCG = ();until ($count == 64){push(@CCG,[@distances]);++$count;}
	$count = 0; @ACT = ();until ($count == 64){push(@ACT,[@distances]);++$count;}
	$count = 0; @ACC = ();until ($count == 64){push(@ACC,[@distances]);++$count;}
	$count = 0; @ACA = ();until ($count == 64){push(@ACA,[@distances]);++$count;}
	$count = 0; @ACG = ();until ($count == 64){push(@ACG,[@distances]);++$count;}
	$count = 0; @GCT = ();until ($count == 64){push(@GCT,[@distances]);++$count;}
	$count = 0; @GCC = ();until ($count == 64){push(@GCC,[@distances]);++$count;}
	$count = 0; @GCA = ();until ($count == 64){push(@GCA,[@distances]);++$count;}
	$count = 0; @GCG = ();until ($count == 64){push(@GCG,[@distances]);++$count;}
	$count = 0; @TAT = ();until ($count == 64){push(@TAT,[@distances]);++$count;}
	$count = 0; @TAC = ();until ($count == 64){push(@TAC,[@distances]);++$count;}
	$count = 0; @CAT = ();until ($count == 64){push(@CAT,[@distances]);++$count;}
	$count = 0; @CAC = ();until ($count == 64){push(@CAC,[@distances]);++$count;}
	$count = 0; @CAA = ();until ($count == 64){push(@CAA,[@distances]);++$count;}
	$count = 0; @CAG = ();until ($count == 64){push(@CAG,[@distances]);++$count;}
	$count = 0; @AAT = ();until ($count == 64){push(@AAT,[@distances]);++$count;}
	$count = 0; @AAC = ();until ($count == 64){push(@AAC,[@distances]);++$count;}
	$count = 0; @AAA = ();until ($count == 64){push(@AAA,[@distances]);++$count;}
	$count = 0; @AAG = ();until ($count == 64){push(@AAG,[@distances]);++$count;}
	$count = 0; @GAT = ();until ($count == 64){push(@GAT,[@distances]);++$count;}
	$count = 0; @GAC = ();until ($count == 64){push(@GAC,[@distances]);++$count;}
	$count = 0; @GAA = ();until ($count == 64){push(@GAA,[@distances]);++$count;}
	$count = 0; @GAG = ();until ($count == 64){push(@GAG,[@distances]);++$count;}
	$count = 0; @TGT = ();until ($count == 64){push(@TGT,[@distances]);++$count;}
	$count = 0; @TGC = ();until ($count == 64){push(@TGC,[@distances]);++$count;}
	$count = 0; @TGG = ();until ($count == 64){push(@TGG,[@distances]);++$count;}
	$count = 0; @CGT = ();until ($count == 64){push(@CGT,[@distances]);++$count;}
	$count = 0; @CGC = ();until ($count == 64){push(@CGC,[@distances]);++$count;}
	$count = 0; @CGA = ();until ($count == 64){push(@CGA,[@distances]);++$count;}
	$count = 0; @CGG = ();until ($count == 64){push(@CGG,[@distances]);++$count;}
	$count = 0; @AGA = ();until ($count == 64){push(@AGA,[@distances]);++$count;}
	$count = 0; @AGG = ();until ($count == 64){push(@AGG,[@distances]);++$count;}
	$count = 0; @GGT = ();until ($count == 64){push(@GGT,[@distances]);++$count;}
	$count = 0; @GGC = ();until ($count == 64){push(@GGC,[@distances]);++$count;}
	$count = 0; @GGA = ();until ($count == 64){push(@GGA,[@distances]);++$count;}
	$count = 0; @GGG = ();until ($count == 64){push(@GGG,[@distances]);++$count;}
	$count = 0; @TAA = ();until ($count == 64){push(@TAA,[@distances]);++$count;}
	$count = 0; @TAG = ();until ($count == 64){push(@TAG,[@distances]);++$count;}
	$count = 0; @TGA = ();until ($count == 64){push(@TGA,[@distances]);++$count;}
	
	$count = 0; @TTT_AA = ();until ($count == 21){push(@TTT_AA,[@distances]);++$count;}
	$count = 0; @TTC_AA = ();until ($count == 21){push(@TTC_AA,[@distances]);++$count;}
	$count = 0; @TTA_AA = ();until ($count == 21){push(@TTA_AA,[@distances]);++$count;}
	$count = 0; @TTG_AA = ();until ($count == 21){push(@TTG_AA,[@distances]);++$count;}
	$count = 0; @CTT_AA = ();until ($count == 21){push(@CTT_AA,[@distances]);++$count;}
	$count = 0; @CTC_AA = ();until ($count == 21){push(@CTC_AA,[@distances]);++$count;}
	$count = 0; @CTA_AA = ();until ($count == 21){push(@CTA_AA,[@distances]);++$count;}
	$count = 0; @CTG_AA = ();until ($count == 21){push(@CTG_AA,[@distances]);++$count;}
	$count = 0; @ATT_AA = ();until ($count == 21){push(@ATT_AA,[@distances]);++$count;}
	$count = 0; @ATC_AA = ();until ($count == 21){push(@ATC_AA,[@distances]);++$count;}
	$count = 0; @ATA_AA = ();until ($count == 21){push(@ATA_AA,[@distances]);++$count;}
	$count = 0; @ATG_AA = ();until ($count == 21){push(@ATG_AA,[@distances]);++$count;}
	$count = 0; @GTT_AA = ();until ($count == 21){push(@GTT_AA,[@distances]);++$count;}
	$count = 0; @GTC_AA = ();until ($count == 21){push(@GTC_AA,[@distances]);++$count;}
	$count = 0; @GTA_AA = ();until ($count == 21){push(@GTA_AA,[@distances]);++$count;}
	$count = 0; @GTG_AA = ();until ($count == 21){push(@GTG_AA,[@distances]);++$count;}
	$count = 0; @TCT_AA = ();until ($count == 21){push(@TCT_AA,[@distances]);++$count;}
	$count = 0; @TCC_AA = ();until ($count == 21){push(@TCC_AA,[@distances]);++$count;}
	$count = 0; @TCA_AA = ();until ($count == 21){push(@TCA_AA,[@distances]);++$count;}
	$count = 0; @TCG_AA = ();until ($count == 21){push(@TCG_AA,[@distances]);++$count;}
	$count = 0; @AGT_AA = ();until ($count == 21){push(@AGT_AA,[@distances]);++$count;}
	$count = 0; @AGC_AA = ();until ($count == 21){push(@AGC_AA,[@distances]);++$count;}
	$count = 0; @CCT_AA = ();until ($count == 21){push(@CCT_AA,[@distances]);++$count;}
	$count = 0; @CCC_AA = ();until ($count == 21){push(@CCC_AA,[@distances]);++$count;}
	$count = 0; @CCA_AA = ();until ($count == 21){push(@CCA_AA,[@distances]);++$count;}
	$count = 0; @CCG_AA = ();until ($count == 21){push(@CCG_AA,[@distances]);++$count;}
	$count = 0; @ACT_AA = ();until ($count == 21){push(@ACT_AA,[@distances]);++$count;}
	$count = 0; @ACC_AA = ();until ($count == 21){push(@ACC_AA,[@distances]);++$count;}
	$count = 0; @ACA_AA = ();until ($count == 21){push(@ACA_AA,[@distances]);++$count;}
	$count = 0; @ACG_AA = ();until ($count == 21){push(@ACG_AA,[@distances]);++$count;}
	$count = 0; @GCT_AA = ();until ($count == 21){push(@GCT_AA,[@distances]);++$count;}
	$count = 0; @GCC_AA = ();until ($count == 21){push(@GCC_AA,[@distances]);++$count;}
	$count = 0; @GCA_AA = ();until ($count == 21){push(@GCA_AA,[@distances]);++$count;}
	$count = 0; @GCG_AA = ();until ($count == 21){push(@GCG_AA,[@distances]);++$count;}
	$count = 0; @TAT_AA = ();until ($count == 21){push(@TAT_AA,[@distances]);++$count;}
	$count = 0; @TAC_AA = ();until ($count == 21){push(@TAC_AA,[@distances]);++$count;}
	$count = 0; @CAT_AA = ();until ($count == 21){push(@CAT_AA,[@distances]);++$count;}
	$count = 0; @CAC_AA = ();until ($count == 21){push(@CAC_AA,[@distances]);++$count;}
	$count = 0; @CAA_AA = ();until ($count == 21){push(@CAA_AA,[@distances]);++$count;}
	$count = 0; @CAG_AA = ();until ($count == 21){push(@CAG_AA,[@distances]);++$count;}
	$count = 0; @AAT_AA = ();until ($count == 21){push(@AAT_AA,[@distances]);++$count;}
	$count = 0; @AAC_AA = ();until ($count == 21){push(@AAC_AA,[@distances]);++$count;}
	$count = 0; @AAA_AA = ();until ($count == 21){push(@AAA_AA,[@distances]);++$count;}
	$count = 0; @AAG_AA = ();until ($count == 21){push(@AAG_AA,[@distances]);++$count;}
	$count = 0; @GAT_AA = ();until ($count == 21){push(@GAT_AA,[@distances]);++$count;}
	$count = 0; @GAC_AA = ();until ($count == 21){push(@GAC_AA,[@distances]);++$count;}
	$count = 0; @GAA_AA = ();until ($count == 21){push(@GAA_AA,[@distances]);++$count;}
	$count = 0; @GAG_AA = ();until ($count == 21){push(@GAG_AA,[@distances]);++$count;}
	$count = 0; @TGT_AA = ();until ($count == 21){push(@TGT_AA,[@distances]);++$count;}
	$count = 0; @TGC_AA = ();until ($count == 21){push(@TGC_AA,[@distances]);++$count;}
	$count = 0; @TGG_AA = ();until ($count == 21){push(@TGG_AA,[@distances]);++$count;}
	$count = 0; @CGT_AA = ();until ($count == 21){push(@CGT_AA,[@distances]);++$count;}
	$count = 0; @CGC_AA = ();until ($count == 21){push(@CGC_AA,[@distances]);++$count;}
	$count = 0; @CGA_AA = ();until ($count == 21){push(@CGA_AA,[@distances]);++$count;}
	$count = 0; @CGG_AA = ();until ($count == 21){push(@CGG_AA,[@distances]);++$count;}
	$count = 0; @AGA_AA = ();until ($count == 21){push(@AGA_AA,[@distances]);++$count;}
	$count = 0; @AGG_AA = ();until ($count == 21){push(@AGG_AA,[@distances]);++$count;}
	$count = 0; @GGT_AA = ();until ($count == 21){push(@GGT_AA,[@distances]);++$count;}
	$count = 0; @GGC_AA = ();until ($count == 21){push(@GGC_AA,[@distances]);++$count;}
	$count = 0; @GGA_AA = ();until ($count == 21){push(@GGA_AA,[@distances]);++$count;}
	$count = 0; @GGG_AA = ();until ($count == 21){push(@GGG_AA,[@distances]);++$count;}
	$count = 0; @TAA_AA = ();until ($count == 21){push(@TAA_AA,[@distances]);++$count;}
	$count = 0; @TAG_AA = ();until ($count == 21){push(@TAG_AA,[@distances]);++$count;}
	$count = 0; @TGA_AA = ();until ($count == 21){push(@TGA_AA,[@distances]);++$count;}

print OUTPUT "\t";
foreach $AA (@AA){print OUTPUT "\t",$AA;}
print OUTPUT "\n";
print OUTPUT "\t";
foreach $codon (@CodonTypes){print OUTPUT "\t",$codon;}
print OUTPUT "\n";



$sequences = 0;#total sequences
$badgenes = 0;#sequences with incomplete reading frames
$badcodon = 0;#unrecognized codons (not ATGC)
while ($fastaline = <CDSFILE>){

	if ($fastaline =~ ">"){
	
		$cdsseq = <CDSFILE>;
		chomp $cdsseq;
		++$sequences;
		
		#Make the sequence uppercase
		$cdsseq = uc $cdsseq;
		
		#print "sequence $sequences\n";
		$dnalength = length $cdsseq;
		$codons = $dnalength/3;
		if ($codons == int($codons)){
			
			$cdn1num = 0;#number of current codon
			until ($cdn1num == $codons){
				$cdn1start = 3*$cdn1num;
				$codon1 = substr($cdsseq,$cdn1start,3);
				$bad = 0;
				
				#number for codon 1 in counting array:
				if ($codon1 eq "TTT"){@CurrentCodon = @TTT;@CurrentAA = @TTT_AA;}
				elsif ($codon1 eq "TTC"){@CurrentCodon = @TTC;@CurrentAA = @TTC_AA;}
				elsif ($codon1 eq "TTA"){@CurrentCodon = @TTA;@CurrentAA = @TTA_AA;}
				elsif ($codon1 eq "TTG"){@CurrentCodon = @TTG;@CurrentAA = @TTG_AA;}
				elsif ($codon1 eq "CTT"){@CurrentCodon = @CTT;@CurrentAA = @CTT_AA;}
				elsif ($codon1 eq "CTC"){@CurrentCodon = @CTC;@CurrentAA = @CTC_AA;}
				elsif ($codon1 eq "CTA"){@CurrentCodon = @CTA;@CurrentAA = @CTA_AA;}
				elsif ($codon1 eq "CTG"){@CurrentCodon = @CTG;@CurrentAA = @CTG_AA;}
				elsif ($codon1 eq "ATT"){@CurrentCodon = @ATT;@CurrentAA = @ATT_AA;}
				elsif ($codon1 eq "ATC"){@CurrentCodon = @ATC;@CurrentAA = @ATC_AA;}
				elsif ($codon1 eq "ATA"){@CurrentCodon = @ATA;@CurrentAA = @ATA_AA;}
				elsif ($codon1 eq "ATG"){@CurrentCodon = @ATG;@CurrentAA = @ATG_AA;}
				elsif ($codon1 eq "GTT"){@CurrentCodon = @GTT;@CurrentAA = @GTT_AA;}
				elsif ($codon1 eq "GTC"){@CurrentCodon = @GTC;@CurrentAA = @GTC_AA;}
				elsif ($codon1 eq "GTA"){@CurrentCodon = @GTA;@CurrentAA = @GTA_AA;}
				elsif ($codon1 eq "GTG"){@CurrentCodon = @GTG;@CurrentAA = @GTG_AA;}
				elsif ($codon1 eq "TCT"){@CurrentCodon = @TCT;@CurrentAA = @TCT_AA;}
				elsif ($codon1 eq "TCC"){@CurrentCodon = @TCC;@CurrentAA = @TCC_AA;}
				elsif ($codon1 eq "TCA"){@CurrentCodon = @TCA;@CurrentAA = @TCA_AA;}
				elsif ($codon1 eq "TCG"){@CurrentCodon = @TCG;@CurrentAA = @TCG_AA;}
				elsif ($codon1 eq "AGT"){@CurrentCodon = @AGT;@CurrentAA = @AGT_AA;}
				elsif ($codon1 eq "AGC"){@CurrentCodon = @AGC;@CurrentAA = @AGC_AA;}
				elsif ($codon1 eq "CCT"){@CurrentCodon = @CCT;@CurrentAA = @CCT_AA;}
				elsif ($codon1 eq "CCC"){@CurrentCodon = @CCC;@CurrentAA = @CCC_AA;}
				elsif ($codon1 eq "CCA"){@CurrentCodon = @CCA;@CurrentAA = @CCA_AA;}
				elsif ($codon1 eq "CCG"){@CurrentCodon = @CCG;@CurrentAA = @CCG_AA;}
				elsif ($codon1 eq "ACT"){@CurrentCodon = @ACT;@CurrentAA = @ACT_AA;}
				elsif ($codon1 eq "ACC"){@CurrentCodon = @ACC;@CurrentAA = @ACC_AA;}
				elsif ($codon1 eq "ACA"){@CurrentCodon = @ACA;@CurrentAA = @ACA_AA;}
				elsif ($codon1 eq "ACG"){@CurrentCodon = @ACG;@CurrentAA = @ACG_AA;}
				elsif ($codon1 eq "GCT"){@CurrentCodon = @GCT;@CurrentAA = @GCT_AA;}
				elsif ($codon1 eq "GCC"){@CurrentCodon = @GCC;@CurrentAA = @GCC_AA;}
				elsif ($codon1 eq "GCA"){@CurrentCodon = @GCA;@CurrentAA = @GCA_AA;}
				elsif ($codon1 eq "GCG"){@CurrentCodon = @GCG;@CurrentAA = @GCG_AA;}
				elsif ($codon1 eq "TAT"){@CurrentCodon = @TAT;@CurrentAA = @TAT_AA;}
				elsif ($codon1 eq "TAC"){@CurrentCodon = @TAC;@CurrentAA = @TAC_AA;}
				elsif ($codon1 eq "CAT"){@CurrentCodon = @CAT;@CurrentAA = @CAT_AA;}
				elsif ($codon1 eq "CAC"){@CurrentCodon = @CAC;@CurrentAA = @CAC_AA;}
				elsif ($codon1 eq "CAA"){@CurrentCodon = @CAA;@CurrentAA = @CAA_AA;}
				elsif ($codon1 eq "CAG"){@CurrentCodon = @CAG;@CurrentAA = @CAG_AA;}
				elsif ($codon1 eq "AAT"){@CurrentCodon = @AAT;@CurrentAA = @AAT_AA;}
				elsif ($codon1 eq "AAC"){@CurrentCodon = @AAC;@CurrentAA = @AAC_AA;}
				elsif ($codon1 eq "AAA"){@CurrentCodon = @AAA;@CurrentAA = @AAA_AA;}
				elsif ($codon1 eq "AAG"){@CurrentCodon = @AAG;@CurrentAA = @AAG_AA;}
				elsif ($codon1 eq "GAT"){@CurrentCodon = @GAT;@CurrentAA = @GAT_AA;}
				elsif ($codon1 eq "GAC"){@CurrentCodon = @GAC;@CurrentAA = @GAC_AA;}
				elsif ($codon1 eq "GAA"){@CurrentCodon = @GAA;@CurrentAA = @GAA_AA;}
				elsif ($codon1 eq "GAG"){@CurrentCodon = @GAG;@CurrentAA = @GAG_AA;}
				elsif ($codon1 eq "TGT"){@CurrentCodon = @TGT;@CurrentAA = @TGT_AA;}
				elsif ($codon1 eq "TGC"){@CurrentCodon = @TGC;@CurrentAA = @TGC_AA;}
				elsif ($codon1 eq "TGG"){@CurrentCodon = @TGG;@CurrentAA = @TGG_AA;}
				elsif ($codon1 eq "CGT"){@CurrentCodon = @CGT;@CurrentAA = @CGT_AA;}
				elsif ($codon1 eq "CGC"){@CurrentCodon = @CGC;@CurrentAA = @CGC_AA;}
				elsif ($codon1 eq "CGA"){@CurrentCodon = @CGA;@CurrentAA = @CGA_AA;}
				elsif ($codon1 eq "CGG"){@CurrentCodon = @CGG;@CurrentAA = @CGG_AA;}
				elsif ($codon1 eq "AGA"){@CurrentCodon = @AGA;@CurrentAA = @AGA_AA;}
				elsif ($codon1 eq "AGG"){@CurrentCodon = @AGG;@CurrentAA = @AGG_AA;}
				elsif ($codon1 eq "GGT"){@CurrentCodon = @GGT;@CurrentAA = @GGT_AA;}
				elsif ($codon1 eq "GGC"){@CurrentCodon = @GGC;@CurrentAA = @GGC_AA;}
				elsif ($codon1 eq "GGA"){@CurrentCodon = @GGA;@CurrentAA = @GGA_AA;}
				elsif ($codon1 eq "GGG"){@CurrentCodon = @GGG;@CurrentAA = @GGG_AA;}
				elsif ($codon1 eq "TAA"){@CurrentCodon = @TAA;@CurrentAA = @TAA_AA;}
				elsif ($codon1 eq "TAG"){@CurrentCodon = @TAG;@CurrentAA = @TAG_AA;}
				elsif ($codon1 eq "TGA"){@CurrentCodon = @TGA;@CurrentAA = @TGA_AA;}
				else {++$bad;}
				
				if ($bad == 0){
					
					$cdn2num = $cdn1num - $half;#number of first codon in window
					if ($cdn2num < 0){$cdn2num = 0;}
					
					$endnum = $cdn1num + $half;#number of last codon in window
					if ($endnum > $codons - 1){$endnum = $codons - 1;}
					
					until ($cdn2num > $endnum){
						$cdn2start = 3*$cdn2num;
						$bad2 = 0;
						$codon2 = substr($cdsseq,$cdn2start,3);		
						$distance = $cdn2num - $cdn1num;#distance of cdn1 from cdn2
						$arraydist = $distance + $half;#convert distance to array element
						if ($codon2 eq "TTT"){$CDN2ID =0;$AA2ID = 0;}
						elsif ($codon2 eq "TTC"){$CDN2ID =1;$AA2ID = 0;}
						elsif ($codon2 eq "TTA"){$CDN2ID =2;$AA2ID = 1;}
						elsif ($codon2 eq "TTG"){$CDN2ID =3;$AA2ID = 1;}
						elsif ($codon2 eq "CTT"){$CDN2ID =4;$AA2ID = 1;}
						elsif ($codon2 eq "CTC"){$CDN2ID =5;$AA2ID = 1;}
						elsif ($codon2 eq "CTA"){$CDN2ID =6;$AA2ID = 1;}
						elsif ($codon2 eq "CTG"){$CDN2ID =7;$AA2ID = 1;}
						elsif ($codon2 eq "ATT"){$CDN2ID =8;$AA2ID = 2;}
						elsif ($codon2 eq "ATC"){$CDN2ID =9;$AA2ID = 2;}
						elsif ($codon2 eq "ATA"){$CDN2ID =10;$AA2ID = 2;}
						elsif ($codon2 eq "ATG"){$CDN2ID =11;$AA2ID = 3;}
						elsif ($codon2 eq "GTT"){$CDN2ID =12;$AA2ID = 4;}
						elsif ($codon2 eq "GTC"){$CDN2ID =13;$AA2ID = 4;}
						elsif ($codon2 eq "GTA"){$CDN2ID =14;$AA2ID = 4;}
						elsif ($codon2 eq "GTG"){$CDN2ID =15;$AA2ID = 4;}
						elsif ($codon2 eq "TCT"){$CDN2ID =16;$AA2ID = 5;}
						elsif ($codon2 eq "TCC"){$CDN2ID =17;$AA2ID = 5;}
						elsif ($codon2 eq "TCA"){$CDN2ID =18;$AA2ID = 5;}
						elsif ($codon2 eq "TCG"){$CDN2ID =19;$AA2ID = 5;}
						elsif ($codon2 eq "AGT"){$CDN2ID =20;$AA2ID = 5;}
						elsif ($codon2 eq "AGC"){$CDN2ID =21;$AA2ID = 5;}
						elsif ($codon2 eq "CCT"){$CDN2ID =22;$AA2ID = 6;}
						elsif ($codon2 eq "CCC"){$CDN2ID =23;$AA2ID = 6;}
						elsif ($codon2 eq "CCA"){$CDN2ID =24;$AA2ID = 6;}
						elsif ($codon2 eq "CCG"){$CDN2ID =25;$AA2ID = 6;}
						elsif ($codon2 eq "ACT"){$CDN2ID =26;$AA2ID = 7;}
						elsif ($codon2 eq "ACC"){$CDN2ID =27;$AA2ID = 7;}
						elsif ($codon2 eq "ACA"){$CDN2ID =28;$AA2ID = 7;}
						elsif ($codon2 eq "ACG"){$CDN2ID =29;$AA2ID = 7;}
						elsif ($codon2 eq "GCT"){$CDN2ID =30;$AA2ID = 8;}
						elsif ($codon2 eq "GCC"){$CDN2ID =31;$AA2ID = 8;}
						elsif ($codon2 eq "GCA"){$CDN2ID =32;$AA2ID = 8;}
						elsif ($codon2 eq "GCG"){$CDN2ID =33;$AA2ID = 8;}
						elsif ($codon2 eq "TAT"){$CDN2ID =34;$AA2ID = 9;}
						elsif ($codon2 eq "TAC"){$CDN2ID =35;$AA2ID = 9;}
						elsif ($codon2 eq "CAT"){$CDN2ID =36;$AA2ID = 10;}
						elsif ($codon2 eq "CAC"){$CDN2ID =37;$AA2ID = 10;}
						elsif ($codon2 eq "CAA"){$CDN2ID =38;$AA2ID = 11;}
						elsif ($codon2 eq "CAG"){$CDN2ID =39;$AA2ID = 11;}
						elsif ($codon2 eq "AAT"){$CDN2ID =40;$AA2ID = 12;}
						elsif ($codon2 eq "AAC"){$CDN2ID =41;$AA2ID = 12;}
						elsif ($codon2 eq "AAA"){$CDN2ID =42;$AA2ID = 13;}
						elsif ($codon2 eq "AAG"){$CDN2ID =43;$AA2ID = 13;}
						elsif ($codon2 eq "GAT"){$CDN2ID =44;$AA2ID = 14;}
						elsif ($codon2 eq "GAC"){$CDN2ID =45;$AA2ID = 14;}
						elsif ($codon2 eq "GAA"){$CDN2ID =46;$AA2ID = 15;}
						elsif ($codon2 eq "GAG"){$CDN2ID =47;$AA2ID = 15;}
						elsif ($codon2 eq "TGT"){$CDN2ID =48;$AA2ID = 16;}
						elsif ($codon2 eq "TGC"){$CDN2ID =49;$AA2ID = 16;}
						elsif ($codon2 eq "TGG"){$CDN2ID =50;$AA2ID = 17;}
						elsif ($codon2 eq "CGT"){$CDN2ID =51;$AA2ID = 18;}
						elsif ($codon2 eq "CGC"){$CDN2ID =52;$AA2ID = 18;}
						elsif ($codon2 eq "CGA"){$CDN2ID =53;$AA2ID = 18;}
						elsif ($codon2 eq "CGG"){$CDN2ID =54;$AA2ID = 18;}
						elsif ($codon2 eq "AGA"){$CDN2ID =55;$AA2ID = 18;}
						elsif ($codon2 eq "AGG"){$CDN2ID =56;$AA2ID = 18;}
						elsif ($codon2 eq "GGT"){$CDN2ID =57;$AA2ID = 19;}
						elsif ($codon2 eq "GGC"){$CDN2ID =58;$AA2ID = 19;}
						elsif ($codon2 eq "GGA"){$CDN2ID =59;$AA2ID = 19;}
						elsif ($codon2 eq "GGG"){$CDN2ID =60;$AA2ID = 19;}
						elsif ($codon2 eq "TAA"){$CDN2ID =61;$AA2ID = 20;}
						elsif ($codon2 eq "TAG"){$CDN2ID =62;$AA2ID = 20;}
						elsif ($codon2 eq "TGA"){$CDN2ID =63;$AA2ID = 20;}
						else {++$bad2;}
							
						if ($codon1 eq "TTT"){
							++$TTT[$CDN2ID][$arraydist];
							++$TTT_AA[$AA2ID][$arraydist];
							$aacount = $TTT_AA[$AA2ID][$arraydist];
							$codoncount = $TTT[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "TTC"){
							++$TTC[$CDN2ID][$arraydist];
							++$TTC_AA[$AA2ID][$arraydist];
							$aacount = $TTC_AA[$AA2ID][$arraydist];
							$codoncount = $TTC[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "TTA"){
							++$TTA[$CDN2ID][$arraydist];
							++$TTA_AA[$AA2ID][$arraydist];
							$aacount = $TTA_AA[$AA2ID][$arraydist];
							$codoncount = $TTA[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "TTG"){
							++$TTG[$CDN2ID][$arraydist];
							++$TTG_AA[$AA2ID][$arraydist];
							$aacount = $TTG_AA[$AA2ID][$arraydist];
							$codoncount = $TTG[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "CTT"){
							++$CTT[$CDN2ID][$arraydist];
							++$CTT_AA[$AA2ID][$arraydist];
							$aacount = $CTT_AA[$AA2ID][$arraydist];
							$codoncount = $CTT[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "CTC"){
							++$CTC[$CDN2ID][$arraydist];
							++$CTC_AA[$AA2ID][$arraydist];
							$aacount = $CTC_AA[$AA2ID][$arraydist];
							$codoncount = $CTC[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "CTA"){
							++$CTA[$CDN2ID][$arraydist];
							++$CTA_AA[$AA2ID][$arraydist];
							$aacount = $CTA_AA[$AA2ID][$arraydist];
							$codoncount = $CTA[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "CTG"){
							++$CTG[$CDN2ID][$arraydist];
							++$CTG_AA[$AA2ID][$arraydist];
							$aacount = $CTG_AA[$AA2ID][$arraydist];
							$codoncount = $CTG[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "ATT"){
							++$ATT[$CDN2ID][$arraydist];
							++$ATT_AA[$AA2ID][$arraydist];
							$aacount = $ATT_AA[$AA2ID][$arraydist];
							$codoncount = $ATT[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "ATC"){
							++$ATC[$CDN2ID][$arraydist];
							++$ATC_AA[$AA2ID][$arraydist];
							$aacount = $ATC_AA[$AA2ID][$arraydist];
							$codoncount = $ATC[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "ATA"){
							++$ATA[$CDN2ID][$arraydist];
							++$ATA_AA[$AA2ID][$arraydist];
							$aacount = $ATA_AA[$AA2ID][$arraydist];
							$codoncount = $ATA[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "ATG"){
							++$ATG[$CDN2ID][$arraydist];
							++$ATG_AA[$AA2ID][$arraydist];
							$aacount = $ATG_AA[$AA2ID][$arraydist];
							$codoncount = $ATG[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "GTT"){
							++$GTT[$CDN2ID][$arraydist];
							++$GTT_AA[$AA2ID][$arraydist];
							$aacount = $GTT_AA[$AA2ID][$arraydist];
							$codoncount = $GTT[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "GTC"){
							++$GTC[$CDN2ID][$arraydist];
							++$GTC_AA[$AA2ID][$arraydist];
							$aacount = $GTC_AA[$AA2ID][$arraydist];
							$codoncount = $GTC[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "GTA"){
							++$GTA[$CDN2ID][$arraydist];
							++$GTA_AA[$AA2ID][$arraydist];
							$aacount = $GTA_AA[$AA2ID][$arraydist];
							$codoncount = $GTA[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "GTG"){
							++$GTG[$CDN2ID][$arraydist];
							++$GTG_AA[$AA2ID][$arraydist];
							$aacount = $GTG_AA[$AA2ID][$arraydist];
							$codoncount = $GTG[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "TCT"){
							++$TCT[$CDN2ID][$arraydist];
							++$TCT_AA[$AA2ID][$arraydist];
							$aacount = $TCT_AA[$AA2ID][$arraydist];
							$codoncount = $TCT[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "TCC"){
							++$TCC[$CDN2ID][$arraydist];
							++$TCC_AA[$AA2ID][$arraydist];
							$aacount = $TCC_AA[$AA2ID][$arraydist];
							$codoncount = $TCC[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "TCA"){
							++$TCA[$CDN2ID][$arraydist];
							++$TCA_AA[$AA2ID][$arraydist];
							$aacount = $TCA_AA[$AA2ID][$arraydist];
							$codoncount = $TCA[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "TCG"){
							++$TCG[$CDN2ID][$arraydist];
							++$TCG_AA[$AA2ID][$arraydist];
							$aacount = $TCG_AA[$AA2ID][$arraydist];
							$codoncount = $TCG[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "AGT"){
							++$AGT[$CDN2ID][$arraydist];
							++$AGT_AA[$AA2ID][$arraydist];
							$aacount = $AGT_AA[$AA2ID][$arraydist];
							$codoncount = $AGT[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "AGC"){
							++$AGC[$CDN2ID][$arraydist];
							++$AGC_AA[$AA2ID][$arraydist];
							$aacount = $AGC_AA[$AA2ID][$arraydist];
							$codoncount = $AGC[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "CCT"){
							++$CCT[$CDN2ID][$arraydist];
							++$CCT_AA[$AA2ID][$arraydist];
							$aacount = $CCT_AA[$AA2ID][$arraydist];
							$codoncount = $CCT[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "CCC"){
							++$CCC[$CDN2ID][$arraydist];
							++$CCC_AA[$AA2ID][$arraydist];
							$aacount = $CCC_AA[$AA2ID][$arraydist];
							$codoncount = $CCC[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "CCA"){
							++$CCA[$CDN2ID][$arraydist];
							++$CCA_AA[$AA2ID][$arraydist];
							$aacount = $CCA_AA[$AA2ID][$arraydist];
							$codoncount = $CCA[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "CCG"){
							++$CCG[$CDN2ID][$arraydist];
							++$CCG_AA[$AA2ID][$arraydist];
							$aacount = $CCG_AA[$AA2ID][$arraydist];
							$codoncount = $CCG[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "ACT"){
							++$ACT[$CDN2ID][$arraydist];
							++$ACT_AA[$AA2ID][$arraydist];
							$aacount = $ACT_AA[$AA2ID][$arraydist];
							$codoncount = $ACT[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "ACC"){
							++$ACC[$CDN2ID][$arraydist];
							++$ACC_AA[$AA2ID][$arraydist];
							$aacount = $ACC_AA[$AA2ID][$arraydist];
							$codoncount = $ACC[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "ACA"){
							++$ACA[$CDN2ID][$arraydist];
							++$ACA_AA[$AA2ID][$arraydist];
							$aacount = $ACA_AA[$AA2ID][$arraydist];
							$codoncount = $ACA[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "ACG"){
							++$ACG[$CDN2ID][$arraydist];
							++$ACG_AA[$AA2ID][$arraydist];
							$aacount = $ACG_AA[$AA2ID][$arraydist];
							$codoncount = $ACG[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "GCT"){
							++$GCT[$CDN2ID][$arraydist];
							++$GCT_AA[$AA2ID][$arraydist];
							$aacount = $GCT_AA[$AA2ID][$arraydist];
							$codoncount = $GCT[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "GCC"){
							++$GCC[$CDN2ID][$arraydist];
							++$GCC_AA[$AA2ID][$arraydist];
							$aacount = $GCC_AA[$AA2ID][$arraydist];
							$codoncount = $GCC[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "GCA"){
							++$GCA[$CDN2ID][$arraydist];
							++$GCA_AA[$AA2ID][$arraydist];
							$aacount = $GCA_AA[$AA2ID][$arraydist];
							$codoncount = $GCA[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "GCG"){
							++$GCG[$CDN2ID][$arraydist];
							++$GCG_AA[$AA2ID][$arraydist];
							$aacount = $GCG_AA[$AA2ID][$arraydist];
							$codoncount = $GCG[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "TAT"){
							++$TAT[$CDN2ID][$arraydist];
							++$TAT_AA[$AA2ID][$arraydist];
							$aacount = $TAT_AA[$AA2ID][$arraydist];
							$codoncount = $TAT[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "TAC"){
							++$TAC[$CDN2ID][$arraydist];
							++$TAC_AA[$AA2ID][$arraydist];
							$aacount = $TAC_AA[$AA2ID][$arraydist];
							$codoncount = $TAC[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "CAT"){
							++$CAT[$CDN2ID][$arraydist];
							++$CAT_AA[$AA2ID][$arraydist];
							$aacount = $CAT_AA[$AA2ID][$arraydist];
							$codoncount = $CAT[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "CAC"){
							++$CAC[$CDN2ID][$arraydist];
							++$CAC_AA[$AA2ID][$arraydist];
							$aacount = $CAC_AA[$AA2ID][$arraydist];
							$codoncount = $CAC[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "CAA"){
							++$CAA[$CDN2ID][$arraydist];
							++$CAA_AA[$AA2ID][$arraydist];
							$aacount = $CAA_AA[$AA2ID][$arraydist];
							$codoncount = $CAA[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "CAG"){
							++$CAG[$CDN2ID][$arraydist];
							++$CAG_AA[$AA2ID][$arraydist];
							$aacount = $CAG_AA[$AA2ID][$arraydist];
							$codoncount = $CAG[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "AAT"){
							++$AAT[$CDN2ID][$arraydist];
							++$AAT_AA[$AA2ID][$arraydist];
							$aacount = $AAT_AA[$AA2ID][$arraydist];
							$codoncount = $AAT[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "AAC"){
							++$AAC[$CDN2ID][$arraydist];
							++$AAC_AA[$AA2ID][$arraydist];
							$aacount = $AAC_AA[$AA2ID][$arraydist];
							$codoncount = $AAC[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "AAA"){
							++$AAA[$CDN2ID][$arraydist];
							++$AAA_AA[$AA2ID][$arraydist];
							$aacount = $AAA_AA[$AA2ID][$arraydist];
							$codoncount = $AAA[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "AAG"){
							++$AAG[$CDN2ID][$arraydist];
							++$AAG_AA[$AA2ID][$arraydist];
							$aacount = $AAG_AA[$AA2ID][$arraydist];
							$codoncount = $AAG[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "GAT"){
							++$GAT[$CDN2ID][$arraydist];
							++$GAT_AA[$AA2ID][$arraydist];
							$aacount = $GAT_AA[$AA2ID][$arraydist];
							$codoncount = $GAT[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "GAC"){
							++$GAC[$CDN2ID][$arraydist];
							++$GAC_AA[$AA2ID][$arraydist];
							$aacount = $GAC_AA[$AA2ID][$arraydist];
							$codoncount = $GAC[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "GAA"){
							++$GAA[$CDN2ID][$arraydist];
							++$GAA_AA[$AA2ID][$arraydist];
							$aacount = $GAA_AA[$AA2ID][$arraydist];
							$codoncount = $GAA[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "GAG"){
							++$GAG[$CDN2ID][$arraydist];
							++$GAG_AA[$AA2ID][$arraydist];
							$aacount = $GAG_AA[$AA2ID][$arraydist];
							$codoncount = $GAG[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "TGT"){
							++$TGT[$CDN2ID][$arraydist];
							++$TGT_AA[$AA2ID][$arraydist];
							$aacount = $TGT_AA[$AA2ID][$arraydist];
							$codoncount = $TGT[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "TGC"){
							++$TGC[$CDN2ID][$arraydist];
							++$TGC_AA[$AA2ID][$arraydist];
							$aacount = $TGC_AA[$AA2ID][$arraydist];
							$codoncount = $TGC[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "TGG"){
							++$TGG[$CDN2ID][$arraydist];
							++$TGG_AA[$AA2ID][$arraydist];
							$aacount = $TGG_AA[$AA2ID][$arraydist];
							$codoncount = $TGG[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "CGT"){
							++$CGT[$CDN2ID][$arraydist];
							++$CGT_AA[$AA2ID][$arraydist];
							$aacount = $CGT_AA[$AA2ID][$arraydist];
							$codoncount = $CGT[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "CGC"){
							++$CGC[$CDN2ID][$arraydist];
							++$CGC_AA[$AA2ID][$arraydist];
							$aacount = $CGC_AA[$AA2ID][$arraydist];
							$codoncount = $CGC[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "CGA"){
							++$CGA[$CDN2ID][$arraydist];
							++$CGA_AA[$AA2ID][$arraydist];
							$aacount = $CGA_AA[$AA2ID][$arraydist];
							$codoncount = $CGA[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "CGG"){
							++$CGG[$CDN2ID][$arraydist];
							++$CGG_AA[$AA2ID][$arraydist];
							$aacount = $CGG_AA[$AA2ID][$arraydist];
							$codoncount = $CGG[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "AGA"){
							++$AGA[$CDN2ID][$arraydist];
							++$AGA_AA[$AA2ID][$arraydist];
							$aacount = $AGA_AA[$AA2ID][$arraydist];
							$codoncount = $AGA[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "AGG"){
							++$AGG[$CDN2ID][$arraydist];
							++$AGG_AA[$AA2ID][$arraydist];
							$aacount = $AGG_AA[$AA2ID][$arraydist];
							$codoncount = $AGG[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "GGT"){
							++$GGT[$CDN2ID][$arraydist];
							++$GGT_AA[$AA2ID][$arraydist];
							$aacount = $GGT_AA[$AA2ID][$arraydist];
							$codoncount = $GGT[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "GGC"){
							++$GGC[$CDN2ID][$arraydist];
							++$GGC_AA[$AA2ID][$arraydist];
							$aacount = $GGC_AA[$AA2ID][$arraydist];
							$codoncount = $GGC[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "GGA"){
							++$GGA[$CDN2ID][$arraydist];
							++$GGA_AA[$AA2ID][$arraydist];
							$aacount = $GGA_AA[$AA2ID][$arraydist];
							$codoncount = $GGA[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "GGG"){
							++$GGG[$CDN2ID][$arraydist];
							++$GGG_AA[$AA2ID][$arraydist];
							$aacount = $GGG_AA[$AA2ID][$arraydist];
							$codoncount = $GGG[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "TAA"){
							++$TAA[$CDN2ID][$arraydist];
							++$TAA_AA[$AA2ID][$arraydist];
							$aacount = $TAA_AA[$AA2ID][$arraydist];
							$codoncount = $TAA[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "TAG"){
							++$TAG[$CDN2ID][$arraydist];
							++$TAG_AA[$AA2ID][$arraydist];
							$aacount = $TAG_AA[$AA2ID][$arraydist];
							$codoncount = $TAG[$CDN2ID][$arraydist];}
						elsif ($codon1 eq "TGA"){
							++$TGA[$CDN2ID][$arraydist];
							++$TGA_AA[$AA2ID][$arraydist];
							$aacount = $TGA_AA[$AA2ID][$arraydist];
							$codoncount = $TGA[$CDN2ID][$arraydist];}
						
						++$cdn2num;
					}#until last codon in window
					
				}#if recognized codon
				
				else {
					++$badcodon;
					#print  "$codon1\n"; 
				}#else didn't recognize codon
				
				++$cdn1num;
				
			}#until finished last codon
			
		}#if full reading frame
		
		else {++$badgenes;}#else not full reading frame
		
	}#if new seq
}#while reading fasta

$Codon2Num = 0;
$AAnum = -1;
$lastAA = "oogabooga";
until (scalar @CodonTypes == 0){

	$Codon2 = shift @CodonTypes;
	$AA2 = shift @AA;
	print OUTPUT $AA2,"\t",$Codon2;
	$Codon1Num = 0;
	#print "$Codon2\t$AA2\n";
	
	unless ($lastAA eq $AA2){++$AAnum;$lastAA = $AA2;}
	until ($Codon1Num == 64){
		
		$AAcount = 0;#total occurrences of AA2 within windows with codon1 (except at -1 and 0 distance)
		$cdncount = 0;#total occurrences of codon2 within windows with codon1 (except at -1 and 0 distance)
		$AAcountm1 = 0;#total occurrences of AA2 at -1 distance (before codon1)
		$cdncountm1 = 0;#total occurrences of codon1 at -1 distance (before codon2)
		$dist = 0;
		until ($dist == $window){
			
			
			if ($Codon1Num == 0){$AAc = $TTT_AA[$AAnum][$dist];$cdnc = $TTT[$Codon2Num][$dist];}
			elsif ($Codon1Num == 1){$AAc = $TTC_AA[$AAnum][$dist];$cdnc = $TTC[$Codon2Num][$dist];}
			elsif ($Codon1Num == 2){$AAc = $TTA_AA[$AAnum][$dist];$cdnc = $TTA[$Codon2Num][$dist];}
			elsif ($Codon1Num == 3){$AAc = $TTG_AA[$AAnum][$dist];$cdnc = $TTG[$Codon2Num][$dist];}
			elsif ($Codon1Num == 4){$AAc = $CTT_AA[$AAnum][$dist];$cdnc = $CTT[$Codon2Num][$dist];}
			elsif ($Codon1Num == 5){$AAc = $CTC_AA[$AAnum][$dist];$cdnc = $CTC[$Codon2Num][$dist];}
			elsif ($Codon1Num == 6){$AAc = $CTA_AA[$AAnum][$dist];$cdnc = $CTA[$Codon2Num][$dist];}
			elsif ($Codon1Num == 7){$AAc = $CTG_AA[$AAnum][$dist];$cdnc = $CTG[$Codon2Num][$dist];}
			elsif ($Codon1Num == 8){$AAc = $ATT_AA[$AAnum][$dist];$cdnc = $ATT[$Codon2Num][$dist];}
			elsif ($Codon1Num == 9){$AAc = $ATC_AA[$AAnum][$dist];$cdnc = $ATC[$Codon2Num][$dist];}
			elsif ($Codon1Num == 10){$AAc = $ATA_AA[$AAnum][$dist];$cdnc = $ATA[$Codon2Num][$dist];}
			elsif ($Codon1Num == 11){$AAc = $ATG_AA[$AAnum][$dist];$cdnc = $ATG[$Codon2Num][$dist];}
			elsif ($Codon1Num == 12){$AAc = $GTT_AA[$AAnum][$dist];$cdnc = $GTT[$Codon2Num][$dist];}
			elsif ($Codon1Num == 13){$AAc = $GTC_AA[$AAnum][$dist];$cdnc = $GTC[$Codon2Num][$dist];}
			elsif ($Codon1Num == 14){$AAc = $GTA_AA[$AAnum][$dist];$cdnc = $GTA[$Codon2Num][$dist];}
			elsif ($Codon1Num == 15){$AAc = $GTG_AA[$AAnum][$dist];$cdnc = $GTG[$Codon2Num][$dist];}
			elsif ($Codon1Num == 16){$AAc = $TCT_AA[$AAnum][$dist];$cdnc = $TCT[$Codon2Num][$dist];}
			elsif ($Codon1Num == 17){$AAc = $TCC_AA[$AAnum][$dist];$cdnc = $TCC[$Codon2Num][$dist];}
			elsif ($Codon1Num == 18){$AAc = $TCA_AA[$AAnum][$dist];$cdnc = $TCA[$Codon2Num][$dist];}
			elsif ($Codon1Num == 19){$AAc = $TCG_AA[$AAnum][$dist];$cdnc = $TCG[$Codon2Num][$dist];}
			elsif ($Codon1Num == 20){$AAc = $AGT_AA[$AAnum][$dist];$cdnc = $AGT[$Codon2Num][$dist];}
			elsif ($Codon1Num == 21){$AAc = $AGC_AA[$AAnum][$dist];$cdnc = $AGC[$Codon2Num][$dist];}
			elsif ($Codon1Num == 22){$AAc = $CCT_AA[$AAnum][$dist];$cdnc = $CCT[$Codon2Num][$dist];}
			elsif ($Codon1Num == 23){$AAc = $CCC_AA[$AAnum][$dist];$cdnc = $CCC[$Codon2Num][$dist];}
			elsif ($Codon1Num == 24){$AAc = $CCA_AA[$AAnum][$dist];$cdnc = $CCA[$Codon2Num][$dist];}
			elsif ($Codon1Num == 25){$AAc = $CCG_AA[$AAnum][$dist];$cdnc = $CCG[$Codon2Num][$dist];}
			elsif ($Codon1Num == 26){$AAc = $ACT_AA[$AAnum][$dist];$cdnc = $ACT[$Codon2Num][$dist];}
			elsif ($Codon1Num == 27){$AAc = $ACC_AA[$AAnum][$dist];$cdnc = $ACC[$Codon2Num][$dist];}
			elsif ($Codon1Num == 28){$AAc = $ACA_AA[$AAnum][$dist];$cdnc = $ACA[$Codon2Num][$dist];}
			elsif ($Codon1Num == 29){$AAc = $ACG_AA[$AAnum][$dist];$cdnc = $ACG[$Codon2Num][$dist];}
			elsif ($Codon1Num == 30){$AAc = $GCT_AA[$AAnum][$dist];$cdnc = $GCT[$Codon2Num][$dist];}
			elsif ($Codon1Num == 31){$AAc = $GCC_AA[$AAnum][$dist];$cdnc = $GCC[$Codon2Num][$dist];}
			elsif ($Codon1Num == 32){$AAc = $GCA_AA[$AAnum][$dist];$cdnc = $GCA[$Codon2Num][$dist];}
			elsif ($Codon1Num == 33){$AAc = $GCG_AA[$AAnum][$dist];$cdnc = $GCG[$Codon2Num][$dist];}
			elsif ($Codon1Num == 34){$AAc = $TAT_AA[$AAnum][$dist];$cdnc = $TAT[$Codon2Num][$dist];}
			elsif ($Codon1Num == 35){$AAc = $TAC_AA[$AAnum][$dist];$cdnc = $TAC[$Codon2Num][$dist];}
			elsif ($Codon1Num == 36){$AAc = $CAT_AA[$AAnum][$dist];$cdnc = $CAT[$Codon2Num][$dist];}
			elsif ($Codon1Num == 37){$AAc = $CAC_AA[$AAnum][$dist];$cdnc = $CAC[$Codon2Num][$dist];}
			elsif ($Codon1Num == 38){$AAc = $CAA_AA[$AAnum][$dist];$cdnc = $CAA[$Codon2Num][$dist];}
			elsif ($Codon1Num == 39){$AAc = $CAG_AA[$AAnum][$dist];$cdnc = $CAG[$Codon2Num][$dist];}
			elsif ($Codon1Num == 40){$AAc = $AAT_AA[$AAnum][$dist];$cdnc = $AAT[$Codon2Num][$dist];}
			elsif ($Codon1Num == 41){$AAc = $AAC_AA[$AAnum][$dist];$cdnc = $AAC[$Codon2Num][$dist];}
			elsif ($Codon1Num == 42){$AAc = $AAA_AA[$AAnum][$dist];$cdnc = $AAA[$Codon2Num][$dist];}
			elsif ($Codon1Num == 43){$AAc = $AAG_AA[$AAnum][$dist];$cdnc = $AAG[$Codon2Num][$dist];}
			elsif ($Codon1Num == 44){$AAc = $GAT_AA[$AAnum][$dist];$cdnc = $GAT[$Codon2Num][$dist];}
			elsif ($Codon1Num == 45){$AAc = $GAC_AA[$AAnum][$dist];$cdnc = $GAC[$Codon2Num][$dist];}
			elsif ($Codon1Num == 46){$AAc = $GAA_AA[$AAnum][$dist];$cdnc = $GAA[$Codon2Num][$dist];}
			elsif ($Codon1Num == 47){$AAc = $GAG_AA[$AAnum][$dist];$cdnc = $GAG[$Codon2Num][$dist];}
			elsif ($Codon1Num == 48){$AAc = $TGT_AA[$AAnum][$dist];$cdnc = $TGT[$Codon2Num][$dist];}
			elsif ($Codon1Num == 49){$AAc = $TGC_AA[$AAnum][$dist];$cdnc = $TGC[$Codon2Num][$dist];}
			elsif ($Codon1Num == 50){$AAc = $TGG_AA[$AAnum][$dist];$cdnc = $TGG[$Codon2Num][$dist];}
			elsif ($Codon1Num == 51){$AAc = $CGT_AA[$AAnum][$dist];$cdnc = $CGT[$Codon2Num][$dist];}
			elsif ($Codon1Num == 52){$AAc = $CGC_AA[$AAnum][$dist];$cdnc = $CGC[$Codon2Num][$dist];}
			elsif ($Codon1Num == 53){$AAc = $CGA_AA[$AAnum][$dist];$cdnc = $CGA[$Codon2Num][$dist];}
			elsif ($Codon1Num == 54){$AAc = $CGG_AA[$AAnum][$dist];$cdnc = $CGG[$Codon2Num][$dist];}
			elsif ($Codon1Num == 55){$AAc = $AGA_AA[$AAnum][$dist];$cdnc = $AGA[$Codon2Num][$dist];}
			elsif ($Codon1Num == 56){$AAc = $AGG_AA[$AAnum][$dist];$cdnc = $AGG[$Codon2Num][$dist];}
			elsif ($Codon1Num == 57){$AAc = $GGT_AA[$AAnum][$dist];$cdnc = $GGT[$Codon2Num][$dist];}
			elsif ($Codon1Num == 58){$AAc = $GGC_AA[$AAnum][$dist];$cdnc = $GGC[$Codon2Num][$dist];}
			elsif ($Codon1Num == 59){$AAc = $GGA_AA[$AAnum][$dist];$cdnc = $GGA[$Codon2Num][$dist];}
			elsif ($Codon1Num == 60){$AAc = $GGG_AA[$AAnum][$dist];$cdnc = $GGG[$Codon2Num][$dist];}
			elsif ($Codon1Num == 61){$AAc = $TAA_AA[$AAnum][$dist];$cdnc = $TAA[$Codon2Num][$dist];}
			elsif ($Codon1Num == 62){$AAc = $TAG_AA[$AAnum][$dist];$cdnc = $TAG[$Codon2Num][$dist];}
			elsif ($Codon1Num == 63){$AAc = $TGA_AA[$AAnum][$dist];$cdnc = $TGA[$Codon2Num][$dist];}
			
			
			unless ($dist == $half or $dist == $half - 1){
				$AAcount = $AAcount + $AAc;
				$cdncount = $cdncount + $cdnc;
			}#unless middle of window or -1 position
			
			if ($dist == $half - 1){
				$AAcountm1 = $AAc;
				$cdncountm1 = $cdnc;
			}#if -1 position
			
			++$dist;
			
		}#until all distances within window
		
		if ($AAcount == 0 or $AAcountm1 == 0){$ratio = "NA";}#if no AA1 within window w codon2
		else {
			$avefreq = $cdncount/$AAcount;#usage frequency of codon1 within windows containing codon2
			$freqm1 = $cdncountm1/$AAcountm1;#usage frequency of codon1 immediately before codon2
			if ($avefreq > 0){$ratio = $freqm1/$avefreq;}#enrichment ratio of codon1 before codon2, relative to average frequency of codon1 near codon2
			else {$ratio = "NA";}
			
		}#else find codon2 enrichment ratio for position before codon1
		
		print OUTPUT "\t$ratio";
		
		++$Codon1Num;
	}
	
	++$Codon2Num;
	print OUTPUT "\n";
	
}


print "$sequences sequences, $badgenes incomplete reading frames, $badcodon unrecognized codons\n";

close CDSFILE;
close OUTPUT;
exit;
